﻿
using _250210作业.BLL;
using _250210作业.Model;
using MVCProject.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace _250210作业.API.Controllers
{
    /// <summary>
    /// 分页获取用户数据
    /// </summary>
    /// <param name="page">页码</param>
    /// <param name="limit">每页条数</param>
    /// <param name="name">模糊查询</param>
    /// <returns></returns>
    public class UsersController : ApiController
    {
        // GET: api/Users
        public ResultPageList<UsersDto> Get(int page, int limit, string name )
        {

         
            UsersBLL bll = new UsersBLL();

            int count = 0;
            var list = bll.GetUsersByPage(page, limit, name, out count);

            var data = from u in list
                       select new UsersDto
                       {
                           UserID = u.userID,
                           Name = u.loginName,
                           Sex = u.sex,
                           Birthday = u.birthday,
                           Phone = u.phone,
                           Pwd = u.loginPwd,
                           Status = u.status,
                           RoleID =(int)u.roleID,
                           RoleName = u.Roles.roleName,
                           DeptID = (int)u.Roles.deptID,
                           DeptName = u.Roles.Department.deptName
                       };

            ResultPageList<UsersDto> result = new ResultPageList<UsersDto>();
            result.code = 0;
            result.msg = "ok";
            result.count = count;
            result.data = data.ToList();

            return result;
        }
        // GET: api/Users/5
       


        // POST: api/Users
        public Result Post([FromBody]UsersDto value)
        {
            Users uu = new Users();
            uu.userID = value.UserID;
            uu.loginName = value.Name;
            uu.sex = value.Sex;
            uu.birthday = value.Birthday;
            uu.roleID = value.RoleID;
            //uu.Roles.roleName = value.RoleName;
            uu.phone=value.Phone;
            uu.loginPwd=value.Pwd;
            uu.status = value.Status;
            //uu.Roles.Department.deptID = value.DeptID;
            //uu.Roles.Department.deptName = value.DeptName;



            var bol = new UsersBLL().Add(uu);


            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "ok" : "error";
            return result;
        }

        // PUT: api/Users/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/Users/5
        public void Delete(int id)
        {
        }
    }
}
